﻿using Domain.Process_Procedures;
using Domain.User;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.Process_Procedures.ProcessFlow;
using MES.Server.API.Dto.ProcessProceduresDTO;
using Microsoft.EntityFrameworkCore;
using ReturnValue;

namespace MES.Server.API.Application.Handler.Process_Procedures.ProcessFlow
{
    public class GetProcessFlowHandler : IRequestHandler<GetProcessFlowCommand, ApiPaging<ProcessFlowDTO>>
    {
        private readonly IRepository<ProcessFlowModel> _processFlowRepo;
        private readonly IRepository<AccountModel> _accountRepo;
        private readonly IRepository<DepartmentModel> _departmentRepo;
        private readonly IRepository<ProcessManagementModel> _processManagementRepo;
        public GetProcessFlowHandler(IRepository<ProcessFlowModel> processFlowRepo, IRepository<ProcessManagementModel> processManagementRepo, IRepository<ProcessCategoryModel> processCategoryRepo = null, IRepository<AccountModel> accountRepo = null, IRepository<DepartmentModel> departmentRepo = null)
        {
            _processFlowRepo = processFlowRepo;
            _processManagementRepo = processManagementRepo;
            _accountRepo = accountRepo;
            _departmentRepo = departmentRepo;
        }
        public async Task<ApiPaging<ProcessFlowDTO>> Handle(GetProcessFlowCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiPaging<ProcessFlowDTO>();

            var processflows= _processFlowRepo.GetAll().Where(x=>!x.IsDel);
            var accounts = _accountRepo.GetAll().Where(x => !x.IsDel);
            var departments = _departmentRepo.GetAll().Where(x => !x.IsDel);
            var processmanagements= _processManagementRepo.GetAll().Where(x=>!x.IsDel);

            //工艺流程编号
            if (!string.IsNullOrEmpty(request.FlowCode))
            {
                processflows = processflows.Where(x => x.FlowCode.Contains(request.FlowCode));
            }
            //工艺流程名称
            if (!string.IsNullOrEmpty(request.FlowName))
            {
                processflows = processflows.Where(x => x.FlowName.Contains(request.FlowName));
            }
            //负责人
            if (request.PersonId != null)
            {
                accounts = accounts.Where(x=>x.Id==request.PersonId);
            }
            //所属部门
            if (request.DepartmentId != null)
            {
                departments = departments.Where(x=>x.Id==request.DepartmentId);
            }
            //状态
            if (request.IsActive != null)
            {
                processflows = processflows.Where(x=>x.IsActive==request.IsActive);
            }
            var linq=from a in processflows
                     join b in accounts on a.PersonId equals b.Id
                     join c in departments on a.DepartmentId equals c.Id
                     select new ProcessFlowDTO()
                     {
                         Id=a.Id,
                         FlowCode=a.FlowCode,
                         FlowName=a.FlowName,
                         ProcessCount=a.ProcessCount,
                         PersonId=a.PersonId,
                         PersonName=b.RealName,
                         DepartmentId=a.DepartmentId,
                         DepartmentName=c.DepartmentName,
                         IsActive=a.IsActive,
                         Remarks=a.Remarks
                     };
            res.Totalcount=await linq.CountAsync();
            res.TotalPage=await linq.OrderByDescending(x=>x.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToListAsync();
            res.Message="查询成功";
            res.Code=StatusCode.Success;
            return res;

        }
    }
}
